//
//  SFNewfeatureView.swift
//  SFWeiboPro
//
//  Created by happy on 2017/9/20.
//  Copyright © 2017年 happy. All rights reserved.
//

import UIKit

class SFNewfeatureView: UIView {

    
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var enterButton: UIButton!
    @IBOutlet weak var pageControl: UIPageControl!
    
    /// 进入微博
    @IBAction func enterBtn() {
        removeFromSuperview()
    }
    
    class func newfeatureView() -> SFNewfeatureView {
        
        let lib = UINib(nibName: "SFNewfeatureView", bundle: nil)
        
        let v = lib.instantiate(withOwner: nil, options: nil)[0] as! SFNewfeatureView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }

    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let count = 4
        let rect = UIScreen.main.bounds
        
        print("\(rect)")
        
        for i in 0..<count {
            
            let imageName = "new_feature_\(i + 1)"
            
            let iv = UIImageView(image: UIImage(named: imageName))
            
            iv.frame = CGRect(x: CGFloat(i) * rect.width, y: 0, width: rect.width, height: rect.height)
            
            scrollView.addSubview(iv)
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        
        scrollView.isPagingEnabled = true
        scrollView.bounces = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        //隐藏登录按钮
        enterButton.isHidden = true
    }

}


extension SFNewfeatureView: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let pageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if pageIndex == scrollView.subviews.count {
            removeFromSuperview()
        }
        
        enterButton.isHidden = (pageIndex != scrollView.subviews.count - 1)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        enterButton.isHidden = true
        
        let pageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pageControl.currentPage = pageIndex
        
        pageControl.isHidden = (pageIndex == scrollView.subviews.count)
        
    }
    
}

